package com.cgc.venus.server.job.service;

import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.job.bean.HomeStatsInfo;
import com.cgc.venus.server.job.inter.HomeStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class HomeStatsServiceImpl extends AbstractBaseService implements HomeStatsService{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public HomeStatsInfo stats(boolean isSuperAdmin, String namespace) throws VenusException {
        HomeStatsInfo homeStatsInfo=new HomeStatsInfo();
        Map<String,String> mTmp=null;
        if(isSuperAdmin){
            //统计计算节点
            mTmp=new LinkedHashMap<>();
            mTmp.put("node_count_all","总数:0个");
            mTmp.put("node_count_ex","异常:0个");
            mTmp.put("node_count_today","当天新增:0个");
            homeStatsInfo.setNodeStats(statsQuery1(mTmp,"t_job_stats_node"));
            //统计团队
            mTmp=new LinkedHashMap<>();
            mTmp.put("team_count_all","总数:0个");
            mTmp.put("team_count_stop","停用:0个");
            mTmp.put("team_count_today","当天新增:0个");
            homeStatsInfo.setTeamStats(statsQuery1(mTmp, "t_job_stats_team"));
            //空间统计
            mTmp=new LinkedHashMap<>();
            mTmp.put("ns_count_all","总数:0个");
            mTmp.put("ns_count_stop","停用:0个");
            mTmp.put("ns_count_today","当天新增:0个");
            homeStatsInfo.setNamespaceStats(statsQuery1(mTmp, "t_job_stats_namespace"));
            //app统计
            mTmp=new LinkedHashMap<>();
            mTmp.put("app_count_all","总数:0个");
            mTmp.put("app_count_running","运行中:0个");
            mTmp.put("app_count_close","已关闭:0个");
            mTmp.put("app_count_ex","异常:0个");
            mTmp.put("app_count_today","当天新增:0个");
            homeStatsInfo.setAppStats(statsQuery1(mTmp, "t_job_stats_app"));
            //镜像统计
            mTmp=new LinkedHashMap<>();
            mTmp.put("Images_count_all","总镜像:0个");
//            mTmp.put("Images_pub_count_all","公有镜像:0个");
//            mTmp.put("Images_pub_count_today","当天新增公有镜像:0个");
            mTmp.put("Images_private_count_all","私有镜像:0个");
            mTmp.put("Images_private_count_today","当天新增私有镜像:0个");
            mTmp.put("Images_store_count_all","商店应用:0个");
            mTmp.put("Images_store_count_today","当天新增商店应用:0个");
            homeStatsInfo.setImagesStats(statsQuery1(mTmp, "t_job_stats_images"));
            //存储统计
            mTmp=new LinkedHashMap<>();
            mTmp.put("storage_count_all","总存储大小:0G");
            mTmp.put("storage_count_req","已请求:0G");
            mTmp.put("storage_count_req_using","请求使用中:0G");
            mTmp.put("storage_count_req_unuse","请求未使用:0G");
            mTmp.put("storage_count_req_today","当天新增:0G");
            homeStatsInfo.setStorageStats(statsQuery1(mTmp, "t_job_stats_storage"));


            //统计平台健康
            String sql="select *,date_format(Last_update_time, '%y-%m-%d %T') time from t_job_stats_health ORDER BY Last_update_time desc LIMIT 1";
            List<Map<String, Object>> r1= this.jdbcTemplate.queryForList(sql);
            List<Map<String, String>> lTmp=new ArrayList<>();
            Map<String, String> mTmp1=null;
            String time="";
            if(r1!=null&& r1.size()>0){
                time=r1.get(0).get("time").toString();
                mTmp1=new HashMap<>();
                mTmp1.put("name","k8s服务");
                mTmp1.put("status",r1.get(0).get("Status_k8s").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","jenkins服务");
                mTmp1.put("status",r1.get(0).get("Status_jenkins").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","harbor服务");
                mTmp1.put("status",r1.get(0).get("Status_harbor").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","路由服务");
                mTmp1.put("status",r1.get(0).get("Status_apiroute").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","用户中心");
                mTmp1.put("status",r1.get(0).get("Status_user_center").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","应用中心");
                mTmp1.put("status",r1.get(0).get("Status_app_center").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","监控中心");
                mTmp1.put("status",r1.get(0).get("Status_monitor_center").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","日志中心");
                mTmp1.put("status",r1.get(0).get("Status_log_center").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","CICD中心");
                mTmp1.put("status",r1.get(0).get("Status_ci_center").toString());
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","任务中心");
                mTmp1.put("status",r1.get(0).get("Status_job_center").toString());
                lTmp.add(mTmp1);
//                mTmp1=new HashMap<>();
//                mTmp1.put("name","帮助中心");
//                mTmp1.put("status",r1.get(0).get("Status_help_center").toString());
//                lTmp.add(mTmp1);
            }else{
                time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                mTmp1=new HashMap<>();
                mTmp1.put("name","k8s服务");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","harbor服务");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","路由服务");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","用户中心");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","应用中心");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","监控中心");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","日志中心");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","CICD中心");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
                mTmp1=new HashMap<>();
                mTmp1.put("name","任务中心");
                mTmp1.put("status","0");
                lTmp.add(mTmp1);
//                mTmp1=new HashMap<>();
//                mTmp1.put("name","帮助中心");
//                mTmp1.put("status","0");
//                lTmp.add(mTmp1);
            }
            Map<String, Object> mTmp2=new HashMap<>();
            mTmp2.put("data",lTmp);
            mTmp2.put("last_time",time);
            homeStatsInfo.setHealthStats(mTmp2);

            //统计预警消息
            sql="SELECT count(*) num FROM t_job_alarm WHERE date_format(Create_time, '%y-%m-%d') = date_format(NOW(), '%y-%m-%d')";
            String count= this.jdbcTemplate.queryForMap(sql).get("num").toString();
            sql="SELECT date_format(Create_time, '%y-%m-%d %T') time, Alarm_msg alarmMsg FROM t_job_alarm WHERE date_format(Create_time, '%y-%m-%d') = date_format(NOW(), '%y-%m-%d') ORDER BY Create_time DESC LIMIT 0, 9";
            mTmp2=new HashMap<>();
            mTmp2.put("data",this.jdbcTemplate.queryForList(sql));
            mTmp2.put("num",count);
            homeStatsInfo.setAlarmStats(mTmp2);
        }else{
            //统计空间应用
            mTmp=new LinkedHashMap<>();
            mTmp.put("Ns_app_count_all","总数:0个");
            mTmp.put("Ns_app_count_running","运行中:0个");
            mTmp.put("Ns_app_count_close","已关闭:0个");
            mTmp.put("Ns_app_count_ex","异常:0个");
            mTmp.put("Ns_app_count_today","当天新增:0个");
            homeStatsInfo.setNamespaceAppStats(statsQuery2(mTmp,"t_job_stats_namespace_app",namespace));
            //统计空间镜像
            mTmp=new LinkedHashMap<>();
            mTmp.put("Ns_Images_private_count_all","私有镜像总数:0个");
            mTmp.put("Ns_Images_private_count_today","当天新增私有镜像 :0个");
            homeStatsInfo.setNamespaceImagesStats(statsQuery2(mTmp, "t_job_stats_namespace_images", namespace));
        }

        return homeStatsInfo;
    }

    private Map<String,Object> statsQuery1(Map<String,String> parm,String table){
        Iterator<Map.Entry<String, String>> iterator= parm.entrySet().iterator();
        String sql="select";
        while(iterator.hasNext()){
            Map.Entry entry = iterator.next();
            sql+=" "+entry.getKey()+",";
        }
        sql+="date_format(last_update_time,'%y-%m-%d %T') time from "+table+" order by last_update_time desc LIMIT 1";
        List<Map<String, Object>> r=jdbcTemplate.queryForList(sql);
        iterator= parm.entrySet().iterator();
        List<String> appLegendDatas=new ArrayList<>();
        List<Map<String,Object>> data=new ArrayList<>();
        Map<String,Object> mTmp1=null;
        while(iterator.hasNext()){
            Map.Entry entry = iterator.next();
            BigDecimal value=new BigDecimal(0);
            String name=entry.getValue().toString();
            if(r!=null&&r.size()>0){
                value=new BigDecimal(r.get(0).get(entry.getKey()).toString());
                name=entry.getValue().toString().replaceAll("0", value.toString());
            }
            appLegendDatas.add(name);
            mTmp1=new HashMap<>();
            mTmp1.put("name",name);
            mTmp1.put("value",value);
            data.add(mTmp1);
        }
        String time="";
        if(r!=null&&r.size()>0){
            time=r.get(0).get("time").toString();
        }
        Map<String,Object> statsResult=new HashMap<>();
        statsResult.put("legendData",appLegendDatas);
        statsResult.put("data",data);
        statsResult.put("last_time",time);
        return statsResult;
    }

    private Map<String,Object> statsQuery2(Map<String,String> parm,String table,String namespace){
        Iterator<Map.Entry<String, String>> iterator= parm.entrySet().iterator();
        String sql="select";
        while(iterator.hasNext()){
            Map.Entry entry = iterator.next();
            sql+=" "+entry.getKey()+",";
        }
        sql+="date_format(last_update_time,'%y-%m-%d %T') time from "+table+" where Ns_name=? order by last_update_time desc LIMIT 1";
        List<Object> obj=new ArrayList<Object>();
        obj.add(namespace);
        List<Map<String, Object>> r=jdbcTemplate.queryForList(sql,obj.toArray());
        iterator= parm.entrySet().iterator();
        List<String> appLegendDatas=new ArrayList<>();
        List<Map<String,Object>> data=new ArrayList<>();
        Map<String,Object> mTmp1=null;
        while(iterator.hasNext()){
            Map.Entry entry = iterator.next();
            BigDecimal value=new BigDecimal(0);
            String name=entry.getValue().toString();
            if(r!=null&&r.size()>0){
                value=new BigDecimal(r.get(0).get(entry.getKey()).toString());
                name=entry.getValue().toString().replaceAll("0", value.toString());
            }
            appLegendDatas.add(name);
            mTmp1=new HashMap<>();
            mTmp1.put("name",name);
            mTmp1.put("value",value);
            data.add(mTmp1);
        }
        String time="";
        if(r!=null&&r.size()>0){
            time=r.get(0).get("time").toString();
        }
        Map<String,Object> statsResult=new HashMap<>();
        statsResult.put("legendData",appLegendDatas);
        statsResult.put("data",data);
        statsResult.put("last_time",time);
        return statsResult;
    }

}